
layui.use(['table', 'form', 'laydate', 'jquery', 'element'], function () {
    var table = layui.table;
    var element = layui.element;
    var $ = layui.$;
    var setter = layui.setter;
    var form = layui.form;
    var laydate = layui.laydate;
    // 第一个实例
    var tableIns = table.render({
        elem: '#table'
        , height: 'full-200'
        , url: '/findUserBy' //数据接口
        , toolbar: '#toolbarDemo'
        , cellMinWidth: 80
        , page: true //开启分页
        , request: {
            pageName: 'pageNum' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }
        , cols: [
            [ //表头id
                {type: 'checkbox'}
                , {field: 'name', title: '姓名', align: 'center'}
                , {
                field: 'gender', title: '性别', sort: true, align: 'center', templet: function (d) {
                    if (d.gender == 0) {
                        return "女"
                    } else if (d.gender == 1) {
                        return "男"
                    } else {
                        return ""
                    }
                }
            }
                , {
                field: 'position', title: '职位', align: 'center', templet: function (d) {
                    if (d.position == 1) {
                        return "测试工程师"
                    } else if (d.position == 2) {
                        return "前端开发"
                    } else if (d.position == 3) {
                        return "部门主管"
                    } else if (d.position == 4) {
                        return "行政"
                    } else if (d.position == 0) {
                        return "开发岗位"
                    } else {
                        return ""
                    }
                }
            }
                , {
                field: 'hiredate', title: '入职时间', sort: true, align: 'center', templet: function (d) {
                    var date = new Date(d.hiredate)
                    var month = parseInt(date.getMonth()) + 1
                    return date.getFullYear() + "-" + month + "-" + date.getDate()
                }
            }
                , {
                field: 'type', title: '类别', sort: true, align: 'center', templet: function (d) {
                    if (d.type == 1) {
                        return "管理员"
                    } else if (d.type == 0) {
                        return "员工"
                    } else {
                        return "暂未办理入职"
                    }
                }
            }
                , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 300, align: 'center'}
            ]
        ]
    });
    //日期控件
    laydate.render({
        elem: '.startDate'
    });
    laydate.render({
        elem: '.endDate'
    });
    //头工具栏事件
    table.on('toolbar(table)', function (obj) {
        let checkStatus = table.checkStatus(obj.config.id);
        let data = checkStatus.data;
        switch (obj.event) {
            case 'add':
                layer.open({
                    type: 2
                    , title: "新增"
                    , area: ['50%', '70%']
                    , content: "/view/addUser"
                });
                break;
            case 'delete':
                if(data.length === 0){
                    layer.msg('请先选择一行数据！')
                }else{
                    var selectId =[];
                    for(var i = 0;i<data.length;i++){
                        selectId.push(data[i].id);
                    }
                    layer.confirm('确认删除这些数据吗？', function (index) {
                        layer.close(index);
                        $.ajax({
                            url:'/deleteEmploy'
                            ,type:"get"

                            ,data:{id:selectId.join(",")}
                            ,success:function (data) {
                                layer.alert(data.msg);
                                //重载
                                table.reload('table',{
                                    page:{
                                        curr:1
                                    },
                                });
                            },error:function () {
                                layer.alert("删除失败！");
                            }
                        })

                    });
                }

                break;

        }
        ;
    });
    //查询提交数据并重载
    form.on('submit(formSub)', function (data) {
        var filed = data.field;
        //执行重载
        tableIns.reload({
            where: {
                name: filed.name
                , startDate: filed.startDate
                , endDate: filed.endDate
            }, page: {
                curr: 1
            }
        })
        return false;
    });
    //监听行工具事件
    table.on('tool(table)', function (obj) {
        var data = obj.data;
        //获取到该行id
        var tableId = {"id": data.id};
        if (obj.event === 'del') {
            layer.confirm('确认删除？', function (index) {
                $.ajax({
                    url: '/deleteEmploy'
                    , data: tableId
                    , success: function (data) {
                        layer.alert(data.msg)
                        //执行重载
                        table.reload('table', {
                            page: {
                                curr: 1
                            }
                        })

                    }
                })
            });
        } else if (obj.event === 'edit') {
            layer.open({
                type: 2
                , title: "编辑"
                , area: ['50%', '70%']
                , content: "/view/updateUser?id=" + tableId.id
            });
        } else if (obj.event === 'showLog') {
            layer.open({
                type: 2
                , title: "日志信息"
                , area: ['100%', '100%']
                , content: "/view/showLog?id=" + tableId.id
            });
        }
    });


});
